#include "../../programView/programview.h"
#include "robotmoveinstructionview.h"
#include "ui_robotmoveinstructionview.h"

RobotMoveInstructionView::RobotMoveInstructionView(int styleId,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RobotMoveInstructionView)
{
    ui->setupUi(this);
    initialDisplayStyle(styleId);
    tmpIpVec.resize(4);
}

void RobotMoveInstructionView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        ui->forward_Btn->setGeometry(10,350,70,40);
        ui->save_Btn->setGeometry(360,350,70,40);
        break;
    default:
        break;
    }
}

void RobotMoveInstructionView::getInput()
{
    if(robotMoveBusType == EM_ROBOT_MOVE_BUS_CLIENT)
    {
        serverIP = QString("%1%2%3%4%5%6%7").arg(ui->lineEdit_ip1->text()).arg(".")
                                            .arg(ui->lineEdit_ip2->text()).arg(".")
                                            .arg(ui->lineEdit_ip3->text()).arg(".")
                                            .arg(ui->lineEdit_ip4->text());
    }
    else if(robotMoveBusType == EM_ROBOT_MOVE_BUS_SERVER)
    {
        serverIP = "";
    }
    communicationPort = ui->PORT_Edit->text().toInt();
    sendParameterStr = ui->SendParameter_Edit->text();
    waitParameterStr = ui->waitParameter_Edit->text();
    jmpLableValue = ui->JMP_Label_Value_Edit->text();
    timeOutValue = ui->TimeOut_Value_Edit->text().toInt();
    noteStr = ui->note_lineEdit->text();
}

void RobotMoveInstructionView::updateView()
{
    ui->CLIENT_radioButton->setChecked(false);
    ui->SERVER_radioButton->setChecked(false);

    if(robotMoveBusType == EM_ROBOT_MOVE_BUS_CLIENT)
    {
        ui->CLIENT_radioButton->setChecked(true);
        ui->IP_Label->setText("Server IP:");
        QString tmp_ip = "(\\d+).(\\d+).(\\d+).(\\d+)";
        QRegExp rxIP(tmp_ip);
        rxIP.exactMatch(serverIP);
        ui->lineEdit_ip1->setText(rxIP.cap(1));
        ui->lineEdit_ip2->setText(rxIP.cap(2));
        ui->lineEdit_ip3->setText(rxIP.cap(3));
        ui->lineEdit_ip4->setText(rxIP.cap(4));

        ui->lineEdit_ip1->setEnabled(true);
        ui->lineEdit_ip2->setEnabled(true);
        ui->lineEdit_ip3->setEnabled(true);
        ui->lineEdit_ip4->setEnabled(true);
    }
    else if(robotMoveBusType == EM_ROBOT_MOVE_BUS_SERVER)
    {
        ui->SERVER_radioButton->setChecked(true);
        ui->IP_Label->setText("Local IP:");
        ui->lineEdit_ip1->setText("127");
        ui->lineEdit_ip2->setText("0");
        ui->lineEdit_ip3->setText("0");
        ui->lineEdit_ip4->setText("1");

        ui->lineEdit_ip1->setEnabled(false);
        ui->lineEdit_ip2->setEnabled(false);
        ui->lineEdit_ip3->setEnabled(false);
        ui->lineEdit_ip4->setEnabled(false);
    }
    ui->PORT_Edit->setText(QString::number(communicationPort));
    ui->SendParameter_Edit->setText(sendParameterStr);
    ui->waitParameter_Edit->setText(waitParameterStr);
    ui->JMP_Label_Value_Edit->setText(jmpLableValue);
    ui->TimeOut_Value_Edit->setText(QString::number(timeOutValue));
    ui->note_lineEdit->setText(noteStr);
}

void RobotMoveInstructionView::changeLanguage(int language)
{
    if(1 == language)
    {
        translator.load("/agvwork/AgvHmi/baseBag/hmiProject/language/RobotMoveInstructionEnglish.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }
    ui->retranslateUi(this);
}

void RobotMoveInstructionView::on_clearNote_Btn_clicked()
{
    ui->note_lineEdit->clear();
    emit cleanNote_Signal();
}

void RobotMoveInstructionView::on_save_Btn_clicked()
{
    getInput();
    emit instructionEditFinished_Signal();
    emit saveInstruction_Signal(instructionStr);
}

void RobotMoveInstructionView::on_forward_Btn_clicked()
{
    this->close();
    programView->instructionListFlag = 0;
    programView->editInstructionViewShowOrHide();
}

void RobotMoveInstructionView::on_SERVER_radioButton_clicked()
{
    robotMoveBusType = EM_ROBOT_MOVE_BUS_SERVER;
    ui->IP_Label->setText("Local IP:");
    tmpIpVec[0] = ui->lineEdit_ip1->text().toInt();
    tmpIpVec[1] = ui->lineEdit_ip2->text().toInt();
    tmpIpVec[2] = ui->lineEdit_ip3->text().toInt();
    tmpIpVec[3] = ui->lineEdit_ip4->text().toInt();

    ui->lineEdit_ip1->setText("127");
    ui->lineEdit_ip2->setText("0");
    ui->lineEdit_ip3->setText("0");
    ui->lineEdit_ip4->setText("1");

    ui->lineEdit_ip1->setEnabled(false);
    ui->lineEdit_ip2->setEnabled(false);
    ui->lineEdit_ip3->setEnabled(false);
    ui->lineEdit_ip4->setEnabled(false);
}

void RobotMoveInstructionView::on_CLIENT_radioButton_clicked()
{
    robotMoveBusType = EM_ROBOT_MOVE_BUS_CLIENT;
    ui->IP_Label->setText("Server IP:");

    ui->lineEdit_ip1->setText(QString::number(tmpIpVec[0]));
    ui->lineEdit_ip2->setText(QString::number(tmpIpVec[1]));
    ui->lineEdit_ip3->setText(QString::number(tmpIpVec[2]));
    ui->lineEdit_ip4->setText(QString::number(tmpIpVec[3]));

    ui->lineEdit_ip1->setEnabled(true);
    ui->lineEdit_ip2->setEnabled(true);
    ui->lineEdit_ip3->setEnabled(true);
    ui->lineEdit_ip4->setEnabled(true);
}
